//
//  SLRootViewController.m
//  SulitLoan
//
//  Created by 虞嘉伟 on 2025/2/18.
//


#import "SLRootViewController.h"
#import <YYKit.h>
#import <Masonry.h>
#import <TYPagerController/TYTabPagerBar.h>
#import <TYPagerController/TYPagerController.h>



@interface SLMainTabBarCell : TYTabPagerBarCell
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIImageView *icon;
@property (nonatomic, strong) UILabel *textLabel;
@end
@implementation SLMainTabBarCell

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        self.bgView = [UIView new];
        self.bgView.layer.cornerRadius = 19;
        [self.contentView addSubview:self.bgView];
        [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(self.contentView);
            make.height.mas_equalTo(38);
        }];
        
        self.icon = [UIImageView new];
        self.icon.backgroundColor = UIColor.purpleColor;
        self.icon.layer.cornerRadius = 12;
        [self.bgView addSubview:self.icon];
        [self.icon mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.bgView).offset(8);
            make.centerY.equalTo(self.bgView);
            make.width.height.mas_equalTo(24);
        }];
        
        self.textLabel = [UILabel new];
        //self.textLabel.text = @"首页";
        self.textLabel.font = [UIFont systemFontOfSize:12 weight:700];
        [self.bgView addSubview:self.textLabel];
        [self.textLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.icon.mas_right).offset(8);
            make.right.equalTo(self.bgView.mas_right).inset(16);
            make.centerY.equalTo(self.icon);
        }];
    }
    return self;
}

@end




@interface SLRootViewController ()<TYTabPagerBarDataSource, TYTabPagerBarDelegate, TYPagerControllerDataSource, TYPagerControllerDelegate>
@property (nonatomic, strong) TYTabPagerBar *tabBar;
@property (nonatomic, strong) TYPagerController *pagerController;
@property (nonatomic, assign) NSInteger curIndex;

@property (nonatomic, copy  ) NSArray *titleArray;
@property (nonatomic, strong) NSMutableArray<UINavigationController *> *ctrlArray;
@end

@implementation SLRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.fd_prefersNavigationBarHidden = YES;
    
    self.titleArray = @[@"Home", @"Order", @"Mine"];
    NSArray *ctrlNames = @[@"Home2", @"SLOrder", @"SLMine"];
    self.ctrlArray = [NSMutableArray array];
    for (NSString *title in ctrlNames) {
        UIViewController *ctrl = [NSClassFromString([title stringByAppendingString:@"ViewController"]) new];
        if (!ctrl) {
            ctrl = [UIViewController new];
        }
        UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:ctrl];
        [self.ctrlArray addObject:navCtrl];
    }
    
    UIView *bottomBar = [UIView new];
    [self.view addSubview:bottomBar];
    bottomBar.backgroundColor = UIColor.blackColor;
    [bottomBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(self.view);
        make.height.mas_equalTo(44+SAFE_AREA_INSETS.bottom);
    }];
    
    // 创建 TYTabPagerBar
    self.tabBar = [[TYTabPagerBar alloc] init];
    self.tabBar.dataSource = self;
    self.tabBar.delegate = self;
    [self.tabBar registerClass:[SLMainTabBarCell class] forCellWithReuseIdentifier:[SLMainTabBarCell cellIdentifier]];
    [self.view addSubview:self.tabBar];
    
    // 创建 TYPagerController
    self.pagerController = [[TYPagerController alloc] init];
    self.pagerController.dataSource = self;
    self.pagerController.delegate = self;
    [self addChildViewController:self.pagerController];
    [self.view addSubview:self.pagerController.view];
    [self.pagerController didMoveToParentViewController:self];
    
    // 设置布局
    [self.tabBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(bottomBar);
        make.height.mas_equalTo(44);
    }];
    [self.pagerController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.equalTo(self.view);
        make.bottom.equalTo(bottomBar.mas_top);
    }];
    
    // 配置样式
    self.tabBar.layout.barStyle = TYPagerBarStyleNoneView;
//    self.tabBar.layout.progressHeight = 2;
//    self.tabBar.layout.progressColor = [UIColor redColor];
    self.tabBar.layout.cellSpacing = 10;
    self.tabBar.layout.adjustContentCellsCenter = YES;
    self.tabBar.layout.cellWidth = 100;
    
    // 刷新数据
    [self.tabBar reloadData];
    [self.pagerController reloadData];
}

#pragma mark - TYTabPagerBarDataSource

- (NSInteger)numberOfItemsInPagerTabBar {
    return 3;
}

- (UICollectionViewCell<TYTabPagerBarCellProtocol> *)pagerTabBar:(TYTabPagerBar *)pagerTabBar cellForItemAtIndex:(NSInteger)index {
    SLMainTabBarCell *cell = [pagerTabBar dequeueReusableCellWithReuseIdentifier:[SLMainTabBarCell cellIdentifier] forIndex:index];
    cell.textLabel.text = self.titleArray[index];
    
    if (self.curIndex == index) {
        cell.bgView.backgroundColor = UIColorHex(#FDC5E0);
        cell.icon.backgroundColor = UIColorHex(#1B1B1B);
        cell.textLabel.textColor = UIColorHex(#1B1B1B);
    } else {
        cell.bgView.backgroundColor = UIColorHex(#FFFFFF80);
        cell.icon.backgroundColor = UIColorHex(#FFFFFF);
        cell.textLabel.textColor = UIColorHex(#FFFFFF);
    }
    return cell;
}

#pragma mark - TYPagerControllerDataSource

- (NSInteger)numberOfControllersInPagerController {
    return self.titleArray.count;
}

- (UIViewController *)pagerController:(TYPagerController *)pagerController controllerForIndex:(NSInteger)index prefetching:(BOOL)prefetching {
    UIViewController *controller = [[UIViewController alloc] init];
    controller.title = self.titleArray[index];
    UINavigationController *navCtrl = self.ctrlArray[index];
    navCtrl.viewControllers.firstObject.title = self.titleArray[index];
    return navCtrl;
}

#pragma mark - TYTabPagerBarDelegate

- (void)pagerTabBar:(TYTabPagerBar *)pagerTabBar didSelectItemAtIndex:(NSInteger)index {
    self.curIndex = index;
    [pagerTabBar reloadData];
    [self.pagerController scrollToControllerAtIndex:index animate:YES];
}

#pragma mark - TYPagerControllerDelegate

- (void)pagerController:(TYPagerController *)pagerController didScrollToIndex:(NSInteger)index {
    self.curIndex = index;
    [self.tabBar scrollToItemFromIndex:self.pagerController.curIndex toIndex:index progress:1.0];
}

@end
